<?php
namespace app\home\controller;

use app\home\common\Common;
use app\home\common\Page;
use app\home\common\Pagetwo;
use app\infrastructure\uri\Request;
use app\infrastructure\uri\Uri;
use think\Db;
use think\Hook;

/**
 * Created by PhpStorm.
 * User: YD
 * Date: 2017/4/26
 * Time: 16:52
 */
class Appconfig extends Common
{
    private $client;
    public  function  __construct(){
        parent::__construct();
        $this->client=new Request();
    }
    public function techGuideList()
    {
         $url=Uri::HOST.Uri::SELECTUSERGUIDE;
        $data=$this->client->getArrayData($url);
        if($data['code'] == 0){
            $data=$data['data'];
        }else {
            $data = [];
        }
//        dd($data);
        $this->assign('data',$data);
        return view();
    }
    /**
     *添加新手指引
     */
    public function  addtechguide(){
        if(\think\Request::instance()->isPost()){
            if(empty($_FILES)){
                return json_encode(['code'=>3,'msg'=>'please upload photo','data'=>'please upload photo']);
            }
            if(empty($_POST['pageDef'])){
                return json_encode(['code'=>3,'msg'=>'please input pageDef','data'=>'please input pageDef']);
            }
            $res=$this->uploadFile('file');
            if($res['valid'] == 0){
                $data=[
                    "youtubeID"=>$_POST['youtubeID'],
                    "operator_id"=>session('userid'),
                    "operator_name"=>session('username'),
                    "pageDef"=>$_POST['pageDef'],
                    "firstSecreenPic"=>$res['data'],
                    "plantForm"=>1
                ];
                $createBanerUrl=Uri::HOST.Uri::ADDUSERGUIDE;
                $res=$this->client->getArrayData($createBanerUrl,$data,3);
                $res=json_encode($res);
                return $res;
            }else{
                return json_encode(['code'=>3,'msg'=>'0x300','data'=>$res['data']]);
            }
        }
        return view();
    }

    /**
     * 修改新手指引的状态
     */
    public function edittechguidestatus(){
        $status=$_POST['status'];
        $id=$_POST['uid'];
        $url=Uri::HOST.Uri::UPDATEUSERGUIDE.$id;
        $data=[
            'is_valid'=>$status
        ];
        return $this->client->getArrayData($url,$data,4);
    }

    /**
     * 通用配置
     */
    public function commonConfig(){
        $url=Uri::HOST.Uri::SELECTSYSCONFIG;
        $data=$this->client->getArrayData($url);
        if($data['code'] ==0){
            $data=$data['data'];
        }else{
            $data=[];
        }
        $this->assign('data',$data);
        return view();
    }

    /**
     * 添加通用配置
     */
    public function addCommonConfig(){
        if(\think\Request::instance()->isAjax()){
            $content=$_POST['content'];
            if(empty($_POST['name'])){
                return ["code"=>2,"msg"=>"0x400","data"=>"please input name"];
            }
            if(empty($_POST['content'])){
                return ["code"=>2,"msg"=>"0x400","data"=>"please input content"];
            }
            $content=explode("\r\n",$content);
            $serCon=[];
            foreach($content as $k=>$v){
                $item=explode(":",$v);
                $serCon[$item[0]][]=$item[1];

            }
            $data=[
                "name"=>$_POST['name'],
                "type"=>$_POST['type'],
                "plantfrom"=>$_POST['plantform'],
                "content"=>json_encode($serCon)
            ];
            $url=Uri::HOST.Uri::SELECTSYSCONFIG;
            $res=$this->client->getArrayData($url,$data,3);
            return $res;
        }

      return view();
    }

    /**
     * 修改通用配置
     */
    public function editCommonConfig(){
        if(\think\Request::instance()->isAjax()){
            $content=$_POST['content'];
            $content=trim($content);
            if(empty($_POST['name'])){
                return ["code"=>2,"msg"=>"0x400","data"=>"please input name"];
            }
            if(empty($_POST['content'])){
                return ["code"=>2,"msg"=>"0x400","data"=>"please input content"];
            }
            $content=explode("\r\n",$content);
            $serCon=[];
            foreach($content as $k=>$v){
                $item=explode(":",$v);
                $serCon[$item[0]][]=$item[1];

            }
            $data=[
                "name"=>$_POST['name'],
                "type"=>$_POST['type'],
                "plantfrom"=>$_POST['plantform'],
                "content"=>json_encode($serCon)
            ];
            $url=Uri::HOST.Uri::SELECTSYSCONFIG.'/'.$_POST['id'];
            $res=$this->client->getArrayData($url,$data,4);
            return $res;
        }
        $id=$_GET['uid'];
       //查找旧数据
        $url=Uri::HOST.Uri::SELECTSYSCONFIG.'?id='.$id;
        $oldData=$this->client->getArrayData($url);
        $content=$oldData['data'][0]['content'];
        $content=json_decode($content);
        $str="";
        foreach($content as $k=>$v){
            foreach($v as  $kk=>$vv){
                $str .=$k.":".$vv."\r\n";
            }
        }
        $data=[
            "id"=>$oldData['data'][0]['id'],
            "name"=>$oldData['data'][0]['name'],
            "plantfrom"=>$oldData['data'][0]['plantfrom'],
            "type"=>$oldData['data'][0]['type'],
            "content"=>$str
        ];
        $this->assign('data',$data);
        return view();
    }
}